# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import json

class MyscrapyPipeline:
    def __init__(self):
        self.file = open("./book.json","w",encoding="utf-8")
    
    def __del__(self):
        self.file.close()

    def process_item(self, item, spider):
        item = dict(item)
        js_data = json.dumps(item,ensure_ascii=False)+",\n"
        self.file.write(js_data)
        #使用完管道需要把数据返回给引擎
        return item
